<template>
    <div>
        <div class="search-term">
            <el-form :inline="true" :model="searchInfo" class="demo-form-inline">
                <el-form-item>
                    <el-input
                            placeholder="请输入姓名"
                            v-model="searchInfo.supplierName"
                            clearable
                            size="small"
                    ></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button @click="onSubmit" type="primary" size="small"
                    >搜 索
                    </el-button
                    >
                </el-form-item>
                <el-form-item>
                    <el-button @click="openDialog(0)" size="small" v-if="btns&&btns.includes('add')">
                        新 增
                    </el-button>
                </el-form-item>
            </el-form>
        </div>
        <el-table :data="tableData" style="width: 100%;margin-bottom: 10px;" border>
            <el-table-column prop="supplierName" label="供应商名称" align="center">
            </el-table-column>
            <el-table-column prop="bankAccount" label="银行账号" align="center">
            </el-table-column>
            <el-table-column prop="bankUserName" label="银行户名" align="center">
            </el-table-column>
            <el-table-column prop="bankOpen" label="开户行" align="center">
            </el-table-column>
            <el-table-column prop="material" label="教材类型" align="center">
            </el-table-column>
            <el-table-column fixed="right" label="操作" align="center" width="180px">
                <template slot-scope="scope">
                    <el-button
                            @click="openDialog(scope.row)"
                            size="small"
                            type="primary"
                            icon="el-icon-edit"
                            v-if="btns&&btns.includes('edit')"
                    >编辑
                    </el-button>
                    <el-button
                            @click="deleteSupplier(scope.row.supplierId)"
                            size="small"
                            type="danger"
                            icon="el-icon-delete"
                            v-if="btns&&btns.includes('delete')"
                    >删除
                    </el-button
                    >
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
                :current-page="currPage"
                :page-size="pageSize"
                :page-sizes="[10, 20, 30, 50, 100]"
                :style="{ float: 'right', padding: '20px' }"
                :total="totalCount"
                @current-change="handlepageChange"
                @size-change="handleSizeChange"
                layout="total, sizes, prev, pager, next, jumper"
        ></el-pagination>

        <!--   新增编辑 -->
        <el-dialog
                :before-close="closeDialog"
                :title="dialogTitle"
                :visible.sync="dialogFormVisible"
                class="dialogs"
        >
            <el-form
                    :model="form"
                    :inline="true"
                    label-width="100px"
                    ref="supplierForm"
            >
                <el-form-item label="供应商名称" prop="supplierName">
                    <el-input
                            autocomplete="off"
                            v-model="form.supplierName"
                            size="small"
                            placeholder="供应商名称"
                            clearable
                    ></el-input>
                </el-form-item>
                <el-form-item label="银行账号" prop="bankAccount">
                    <el-input
                            autocomplete="off"
                            v-model="form.bankAccount"
                            size="small"
                            placeholder="银行账号"
                            clearable
                    ></el-input>
                </el-form-item>
                <el-form-item label="银行户名" prop="bankUserName">
                    <el-input
                            autocomplete="off"
                            v-model="form.bankUserName"
                            size="small"
                            placeholder="银行户名"
                            clearable
                    ></el-input>
                </el-form-item>
                <el-form-item label="开户行" prop="bankOpen">
                    <el-input
                            autocomplete="off"
                            v-model="form.bankOpen"
                            size="small"
                            placeholder="开户行"
                            clearable
                    ></el-input>
                </el-form-item>
                <el-form-item label="教材类型" prop="material">
                    <el-input
                            autocomplete="off"
                            v-model="form.material"
                            size="small"
                            placeholder="教材类型"
                            clearable
                    ></el-input>
                </el-form-item>

            </el-form>
            <div class="dialog-footer" slot="footer">
                <el-button @click="closeDialog" size="small">取 消</el-button>
                <el-button @click="saveOrUpdateSupplier" type="primary" size="small">保 存</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    // 获取列表内容封装在mixins内部  getTableData方法 初始化已封装完成 条件搜索时候 请把条件安好后台定制的结构体字段 放到 this.searchInfo 中即可实现条件搜索
    import {supplierAdd, supplierDelete, supplierEdit, supplierList} from "@/api/supplier";
    import infoList from "@/components/mixins/infoList";

    export default {
        mixins: [infoList],
        data() {
            return {
                searchInfo: {
                    supplierName: ''
                },
                dialogTitle: '新增',
                dialogFormVisible: false,
                doFlag: 0,
                form: {},
                btns: [],
                listApi: supplierList,
            };
        },
        methods: {
            //条件搜索前端看此方法
            onSubmit() {
                this.page = 1;
                this.pageSize = 10;
                this.getTableData();
            },
            closeDialog() {
                // this.$refs.supplierForm.resetFields();
                this.dialogFormVisible = false;
            },
            openDialog(val) {
                this.dialogFormVisible = true;
                if (val !== 0) {
                    this.form = val;
                    this.dialogTitle = "编辑";
                    this.doFlag = 1;
                }
                if (val === 0){
                    this.form = {};
                }
            },
            async saveOrUpdateSupplier() {
                //新增
                if (this.doFlag === 0) {
                    const res = await supplierAdd(this.form);
                    if (res.statusCode === 0) {
                        this.$message({
                            type: "success",
                            message: "添加成功",
                            showClose: true,
                        });
                    }
                }
                //修改
                if (this.doFlag === 1) {
                    const res = await supplierEdit(this.form);
                    if (res.statusCode === 0) {
                        this.$message({
                            type: "success",
                            message: "修改成功",
                            showClose: true,
                        });
                    }
                }
                this.getTableData();
                this.closeDialog();
            },
            async deleteSupplier(val) {
                this.$confirm("此操作将删除供应商, 是否继续?", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning",
                })
                    .then(async () => {
                        const res = await supplierDelete(val);
                        if (res.statusCode === 0) {
                            this.$message({
                                type: "success",
                                message: "删除成功",
                                showClose: true,
                            });
                        }
                        this.getTableData();
                    })
                    .catch(() => {
                        this.$message({
                            type: "info",
                            message: "已取消",
                        });
                    });
            }
        },
        created() {
            this.btns = this.$route.meta.btnList
            this.getTableData()
        },
    };
</script>
<style scoped lang="scss">
    .info {
        color: #409EFF;
    }

    .warning {
        color: #E6A23C;
    }

    .success {
        color: #67c23a;
    }

    .danger {
        color: #f56c6c;
    }

    .button-box {
        // padding: 10px 20px;
        .el-button {
            float: right;
        }
    }

    .el-tag--mini {
        margin-left: 5px;
    }

    .dialogs {
        ::v-deep .el-input-group--append .el-input__inner {
            width: 170px !important;
        }

        ::v-deep .el-dialog__body {
            padding: 15px 20px 10px 20px;
        }

        ::v-deep .el-input__inner {
            width: 225px;
        }

        ::v-deep .el-textarea__inner {
            width: 225px;
        }

        .dialog-footer {
            text-align: center;
        }

        ::v-deep .el-input-number .el-input__inner {
            width: 130px;
        }

        ::v-deep .el-form .el-form-item {
            width: 49%;
            margin: 0;
        }

        ::v-deep .el-form .pics {
            width: 100%;
            margin-bottom: 22px;
        }

        ::v-deep .el-form .small_pic {
            .el-upload--picture-card {
                height: 80px;
                width: 80px;
                line-height: 80px;
            }
        }

        .pics {
            ::v-deep .el-form-item__content {
                width: calc(100% - 112px)
            }
        }

        .el-form .pics ::v-deep .el-form-item__content .el-textarea__inner {
            width: 100%;
        }
    }

    .header-background {
        position: absolute top 0 left 0 width 100% height 100% z-index -1
    }

</style>
